package com.qt.collection.myset;

import com.qt.collection.Student;

import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

public class TreeSetDemo01 {

    public static void main(String[] args) {

        TreeSet<String> ts = new TreeSet<String>();

         TreeSet<Integer> ts2 = new TreeSet<Integer>();

        // 添加元素
        ts2.add(5);
        ts2.add(3);
        ts2.add(4);

        // 遍历元素
        for (Integer integer : ts2) {
            System.out.println(integer);
        }
        System.out.println("---------------");
        Iterator<Integer> iterator = ts2.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }

        System.out.println("---------------");

        ts2.forEach(s -> System.out.println(s));
        System.out.println("=============");

        TreeSet<Student> ts3 = new TreeSet<Student>();
        ts3.add(new Student("lisi", 24));
        ts3.add(new Student("zhangsan", 23));
        ts3.add(new Student("wangwu", 25));

        Iterator<Student> iterator1 = ts3.iterator();
        while (iterator1.hasNext()){
            Student next = iterator1.next();
            System.out.println(next);
        }

        System.out.println("==============");

        TreeSet<Student> ts4 = new TreeSet<Student>((o1, o2)-> {
                int index = o1.getName().length() - o2.getName().length();
                index = index == 0 ? o1.compareTo(o2) : index;
                return index;
        });

        ts4.add(new Student("lisi", 24));
        ts4.add(new Student("zhangsan", 23));
        ts4.add(new Student("wangwu", 25));

        Iterator<Student> it = ts4.iterator();
        while (it.hasNext()){
            System.out.println(it.next());
        }



    }

}
